#include "keyboard.h"
#include "ui_keyboard.h"
#include <QPainter>
#include <QMouseEvent>
#include <iostream>
#include <QtDebug>
#include "ktv/ktv.h"
#include "ktv/model/resource/resource.h"

Keyboard::Keyboard(KTV *k, QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Keyboard)
{
    tables[0] = "q";
    tables[1] = "w";
    tables[2] = "e";
    tables[3] = "r";
    tables[4] = "t";
    tables[5] = "y";
    tables[6] = "u";
    tables[7] = "i";
    tables[8] = "o";
    tables[9] = "quit";
    tables[10] = "a";
    tables[11] = "s";
    tables[12] = "d";
    tables[13] = "f";
    tables[14] = "g";
    tables[15] = "h";
    tables[16] = "j";
    tables[17] = "k";
    tables[18] = "ok";
    tables[19] = "ok";
    tables[20] = "z";
    tables[21] = "x";
    tables[22] = "c";
    tables[23] = "v";
    tables[24] = "b";
    tables[25] = "n";
    tables[26] = "m";
    tables[27] = "p";
    tables[28] = "l";
    tables[29] = "del";
    ktv = k;
    ui->setupUi(this);
    currentPos=1;
    setFixedSize(400, 118);
    keyboard = Resource::getInstance()->keyboard;
    frame = Resource::getInstance()->frame;
    doubleFrame = Resource::getInstance()->doubleFrame;
    name = "";
}

Keyboard::~Keyboard()
{
    delete ui;
}

void Keyboard::changeEvent(QEvent *e)
{
    QMainWindow::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}
void Keyboard::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.setCompositionMode(QPainter::CompositionMode_Source);
    painter.drawPixmap(0,0,*keyboard);
    painter.setCompositionMode(QPainter::CompositionMode_Difference); //Darkern
    if(currentPos<=10)
     painter.drawPixmap((currentPos-1)*40-2,1,*frame);
    else if(currentPos<=20)
    {
      if(currentPos==20)
          painter.drawPixmap((currentPos-12)*40-4,40,*doubleFrame);
      else
          painter.drawPixmap((currentPos-11)*40-2,40,*frame);
    }
    else if(currentPos<=30)
          painter.drawPixmap((currentPos-21)*40-2,79,*frame);
}
void Keyboard::moveRight()
{
    if(currentPos<30)
    {
       if(currentPos==18)
           currentPos+=2;
       else
           currentPos++;
      this->repaint();
    }
}
void Keyboard::moveLeft()
{
    if(currentPos>1)
    {
     if(currentPos==20)
         currentPos-=2;
     else
        currentPos--;
     this->repaint();
    }
}
void Keyboard::moveUp()
{
    if(currentPos>10)
    {
     currentPos-=10;
     if(currentPos==19)
         currentPos=20;
     this->repaint();
    }
}
void Keyboard::moveDown()
{
    if(currentPos<=20)
    {
      if(currentPos==9)
         currentPos+=11;
      else
        currentPos+=10;
     this->repaint();
    }
}

QString Keyboard::getCurrentLetter()
{
    return tables[currentPos-1];
}

void Keyboard::keyPressEvent(QKeyEvent *event)
{
    switch (event->key()) {
    case Qt::Key_Right:
        moveRight();
        break;
    case Qt::Key_Left:
        moveLeft();
        break;
    case Qt::Key_Up:
        moveUp();
        break;
    case Qt::Key_Down:
        moveDown();
        break;
    case Qt::Key_Y:
        QString letter = getCurrentLetter();
        parseLetter(letter);
        break;
    }
}

void Keyboard::select()
{
    QString letter = getCurrentLetter();
    parseLetter(letter);
}

void Keyboard::parseLetter(QString letter)
{
    qDebug() << "keyboard: handle selected letter";
    if (letter == "ok") {
        ktv->setInputFieldText(name);
        currentPos = 1;
        name = "";
        ktv->hideKeyboard();
        return;
    }
    if (letter == "quit") {
        currentPos = 1;
        name = "";
        ktv->hideKeyboard();
        return;
    }
    if (letter == "del") {
        name.remove(name.size()-1, 1);
    } else {
        name.append(letter);
    }
    qDebug() << "name: " << name;
    ktv->setInputFieldText(name);
}
